package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.jwgf.adapter.CommonAdapter;
import com.jwgf.adapter.ViewHolder;
import com.jwgf.common.ViewCache;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.UserLogin;
import com.jwgf.objectbox.UserLogin_;
import com.jwgf.smarowindingmachine_unity.R;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.Box;

public class EquipmentMaintenanceAuthoritySettingFragment extends BaseFragment {

    private LinearLayout normal_table;
    private ListView listView;
    private CommonAdapter adapter;
    private List<UserLogin> list = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_equipment_maintenance_authority_setting);
        listView = (ListView) v.findViewById(R.id.user_list);
        listView.setAdapter(adapter = new CommonAdapter<UserLogin>(
                getActivity(), getData(), R.layout.item_user_list) {

            @Override
            public void convert(final ViewHolder holder,
                                final UserLogin item) {
                holder.setText(R.id.user_id, item.user);
                holder.setText(R.id.write_level, String.valueOf(item.writeLevel));
                holder.setText(R.id.read_level, String.valueOf(item.readLevel));
            }
        });

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        registerForContextMenu(listView);
        listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                                            ContextMenuInfo menuInfo) {
                menu.add(0, 1, 0, R.string.add);
                menu.add(0, 2, 0, R.string.edit);
                menu.add(0, 3, 0, R.string.delete);
            }
        });
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo itemInfo = (AdapterContextMenuInfo) item
                .getMenuInfo();
        switch (item.getItemId()) {
            case 1: {
                UserAuthorityDialog dlg = new UserAuthorityDialog();
                dlg.setCallBack(new Runnable() {

                    @Override
                    public void run() {
                        updateList();
                    }
                });
                dlg.show(getFragmentManager(), "UserAuthorityDialog");
            }
            break;
            case 2: {
                UserAuthorityDialog dlg = new UserAuthorityDialog();
                dlg.setCallBack(new Runnable() {

                    @Override
                    public void run() {
                        updateList();
                    }
                });
                Bundle bd = new Bundle();
                UserLogin user = list.get(itemInfo.position);
                bd.putString(UserLogin.COL_ID, String.valueOf(user.id));
                bd.putString(UserLogin.COL_USER, user.user);
                bd.putString(UserLogin.COL_PASSWORD, user.password);
                bd.putString(UserLogin.COL_WRITE_LEVEL, String.valueOf(user.writeLevel));
                bd.putString(UserLogin.COL_READ_LEVEL, String.valueOf(user.readLevel));
                dlg.setArguments(bd);
                dlg.show(getFragmentManager(), "UserAuthorityDialog");
            }
            break;
            case 3: {
                UserLogin user = list.get(itemInfo.position);
                if (!user.user.equals("Administrator")) {
                    ObjectBox.get().boxFor(UserLogin.class).remove(user);
                    updateList();
                }
            }
            break;
        }
        return super.onContextItemSelected(item);
    }

    private List<UserLogin> getData() {
        list.clear();
        Box<UserLogin> box = ObjectBox.get().boxFor(UserLogin.class);
        List<UserLogin> userList = box.query().notEqual(UserLogin_.user, "Factory").build().find();
        list.addAll(userList);
        return list;
    }

    private void updateList() {
        getData();
        adapter.notifyDataSetChanged();
    }

}
